package com.bee.beemanage.api.controller;

import com.bee.beemanage.api.entity.SysRolePermission;
import com.bee.beemanage.api.entity.input.IdList;
import com.bee.beemanage.api.service.SysRolePermissionService;
import com.bee.beemanage.system.common.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.bee.beemanage.system.common.utils.result.ResultTool.success;

@RestController
@RequestMapping("/bee-manage/sys-role-permission")
@Api(value ="角色权限关联表",tags = "角色权限关联表")
public class SysRolePermissionController {

    @Resource
    private SysRolePermissionService sysRolePermissionService;

    /**
    * 新增角色权限关联表
    * @param sysRolePermission
    * @return
    */
    @ApiOperation(value = "新增角色权限关联表", notes = "新增角色权限关联表")
    @PostMapping
    @PreAuthorize("hasAnyAuthority('sys:rolePermission:save')")
    public Result save(@RequestBody SysRolePermission sysRolePermission) {
        return sysRolePermissionService.insert(sysRolePermission);
    }

    /**
    * 通过roleId和permissionId删除角色权限关联表
    * @param sysRolePermission
    * @return
    */
    @ApiOperation(value = "通过roleId和permissionId删除角色权限关联表", notes = "通过roleId和permissionId删除角色权限关联表")
    @PostMapping("/removeByAll")
    @PreAuthorize("hasAnyAuthority('sys:rolePermission:removeByAll')")
    public Result removeByAll(@RequestBody SysRolePermission sysRolePermission) {
        return success(sysRolePermissionService.removeByAll(sysRolePermission));
    }

    /**
     * 通过roleId和permissionId批量删除角色权限关联表
     * @param sysRolePermissions
     * @return
     */
    @ApiOperation(value = "通过roleId和permissionId删除角色权限关联表", notes = "通过roleId和permissionId删除角色权限关联表")
    @PostMapping("/removeBatch")
    @PreAuthorize("hasAnyAuthority('sys:rolePermission:removeBatch')")
    public Result removeBatch(@RequestBody List<SysRolePermission> sysRolePermissions) {
        return sysRolePermissionService.removeBatch(sysRolePermissions);
    }

    /**
    * 批量新增角色权限关联
    * @param idList
    * @return
    */
    @ApiOperation(value = "批量新增角色权限关联", notes = "批量新增角色权限关联")
    @PostMapping("/editBatch")
    @PreAuthorize("hasAnyAuthority('sys:rolePermission:insertBatch')")
    public Result editBatch(@RequestBody IdList idList) {
        return sysRolePermissionService.editBatch(idList.getId(),idList.getListString());
    }

    @ApiOperation(value = "通过roleId查询所有角色权限关联", notes = "通过roleId查询所有角色权限关联")
    @GetMapping("/{roleId}")
    @PreAuthorize("hasAnyAuthority('sys:rolePermission:getAllByRole')")
    public Result getAllByRole(@PathVariable String roleId) {
        return success(sysRolePermissionService.selectListByRoleID(roleId));
    }
}